<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>02_计算器练习</title>
</head>
<body>
    <input type="text" placeholder="请输入数字1" id="id1"><br>
    <input type="text" placeholder="请输入数字2" id="id2"><br>
    <button onclick="calc('+')">加</button>
    <button onclick="calc('-')">减</button>
    <button onclick="calc('*')">乘</button>
    <button onclick="calc('/')">除</button>
    <h4>运算结果：<span></span></h4>


    <script>
        function calc(o){
            let i1=document.querySelector("#id1").value;
            let i2=document.querySelector("#id2").value;
            n1=parseFloat(i1);
            n2=parseFloat(i2);
            let spane = document.querySelector("span");
            switch (o){
                case '+':
                    spane.innerHTML=n1+n2;
                    break;
                case '-':
                    spane.innerHTML=n1-n2;
                    break;
                case '*':
                    spane.innerHTML=n1*n2;
                    break;
                case '/':
                    if(n2!=0){
                        spane.innerHTML=n1/n2;
                    }else{
                        alert("除数不能为0");
                    }
                    break;
            }
        }
    </script>
</body>
</html>